/* This file is part of the ili2fme project.
 * For more information, please see <http://www.eisenhutinformatik.ch/interlis/ili2fme/>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package ch.so.sogis.interlis;

import java.util.Iterator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;

import ch.interlis.ili2c.metamodel.*;
import ch.ehi.basics.logging.EhiLogger;


/**
 * @author ce
 * @version $Revision: 1.0 $ $Date: 27.07.2006 $
 */
public class ModelUtility {
	private ModelUtility(){};
	/** @return map<String fmeFeatureTypeName, ViewableWrapper wrapper>
	 */
	
	public static HashMap getItfTransferViewables(ch.interlis.ili2c.metamodel.TransferDescription td)
	{
		HashMap ret = new HashMap();
		Iterator modeli = td.iterator();
		while (modeli.hasNext()) { 
			Object mObj = modeli.next();
			if (mObj instanceof Model) {
				Model model = (Model) mObj;
				if (model instanceof TypeModel) {
					continue;
				}
				if (model instanceof PredefinedModel) {
					continue;
				}
				Iterator topici = model.iterator();
				while (topici.hasNext()) {
					Object tObj = topici.next();
					if (tObj instanceof Topic) {
						Topic topic = (Topic) tObj;
						Iterator iter = topic.getViewables().iterator();
						while (iter.hasNext()) {
							Object obj = iter.next();
							if (obj instanceof Viewable) {
								Viewable v = (Viewable) obj;
								if(isPureRefAssoc(v)){
									continue;
								}
								//log.logMessageString("getTransferViewables() leave <"+v+">",IFMELogFile.FME_INFORM);
								String className = v.getScopedName(null);
								ViewableWrapper viewableWrapper = new ViewableWrapper(className, v);
								java.util.List attrv = ch.interlis.iom_j.itf.ModelUtilities.getIli1AttrList((AbstractClassDef) v);
								viewableWrapper.setAttrv(attrv);
								ret.put(viewableWrapper.getFmeFeatureType(),viewableWrapper);
								// set geom attr in wrapper
								Iterator attri = v.getAttributes();
								while (attri.hasNext()) {
									Object attrObj = attri.next();
									if (attrObj instanceof AttributeDef) {
										AttributeDef attr =
											(AttributeDef) attrObj;
										Type type =
											Type.findReal(attr.getDomain());
										if (type instanceof PolylineType 
											|| type instanceof SurfaceOrAreaType 
											|| type instanceof CoordType
											){
												viewableWrapper.setGeomAttr4FME(attr);
												break;
										}
									}
								}
								// add helper tables of surface and area attributes
								attri = v.getAttributes();
								while (attri.hasNext()) {
									Object attrObj = attri.next();
									if (attrObj instanceof AttributeDef) {
										AttributeDef attr =
											(AttributeDef) attrObj;
										Type type =
											Type.findReal(attr.getDomain());
										if (type
											instanceof SurfaceOrAreaType) {
											String name =
												v.getContainer().getScopedName(
													null)
													+ "."
													+ v.getName()
													+ "_"
													+ attr.getName();
											ViewableWrapper wrapper =
												new ViewableWrapper(name);
											wrapper.setGeomAttr4FME(attr);
											ArrayList helper_attrv=new ArrayList();
											helper_attrv.add(new ViewableTransferElement(attr));
											wrapper.setAttrv(helper_attrv);
											ret.put(wrapper.getFmeFeatureType(),wrapper);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return ret;
	}
	
	public static boolean isPureRefAssoc(Viewable v) {
		if(!(v instanceof AssociationDef)){
			return false;
		}
		AssociationDef assoc=(AssociationDef)v;
		// embedded and no attributes/embedded links?
		if(assoc.isLightweight() && 
			!assoc.getAttributes().hasNext()
			&& !assoc.getLightweightAssociations().iterator().hasNext()
			) {
			return true;
		}
		return false;
	}
	

	

	
	
}

